package com.rritw.webtools.action.tool;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.rritw.webtools.action.IpsAction;
import com.rritw.webtools.common.Const;
import com.rritw.webtools.util.NumberUtil;
import com.rritw.webtools.util.StringUtil;

@Controller
@Scope("prototype")
public class WebChartsAction extends IpsAction {

    private static final long   serialVersionUID = 4693100692656659729L;
    /** Input and output data **/
    private String              labels;
    private String              data;
    private String              dataType;
    private String              firstDataIsLabel;
    private String              chartType;

    /** Output data **/
    private String              chartLabels;
    private List<String>        chartDatas       = new ArrayList<String>();
    /** 生成图的上面的Label，如张三、李四等 **/
    private List<String>        chartLabelArr    = new ArrayList<String>();

    private static List<String> fillColor        = new ArrayList<String>();
    private static String[]     colorsArr        = { "#FF6384", "#4BC0C0", "#FFCE56", "#E7E9ED", "#36A2EB", "#F5DEB3",
                                                     "#EEC900", "#CAFF70", "#AB82FF", "#551A8B", "#6CA6CD", "#556B2F",
                                                     "#8B5A00", "#8B8989", "#9932CC", "#98FB98", "#B22222", "#BA55D3",
                                                     "#C67171", "#CD4F39" };
    private static String       colorsStr        = "";

    // 用于页面上随机显示示例
    private int                 randomExample    = (int) (2 * Math.random());

    static {
        for (int i = 0; i < colorsArr.length; i++) {
            fillColor.add(colorsArr[i]);
            colorsStr += "\"" + colorsArr[i] + "\"";
            if (i < colorsArr.length - 1) {
                colorsStr += ",";
            }
        }
    }

    public String index() {
        super.init();
        return SUCCESS;
    }

    public String create() {
        super.init();
        if (StringUtil.isEmpty(data)) {
            return ERROR;
        }
        if (!Const.TRUE.equalsIgnoreCase(firstDataIsLabel) && StringUtil.isEmpty(labels)) {// Label如果没有填，就自动生成
            labels = Const.EMPTY_STRING;
        }

        // 最多列数
        int maxColumns = 0;
        List<String> dataLines = StringUtil.getContentList(data, Const.LINE_SPLITER);
        List<List<String>> dataLinesList = new ArrayList<List<String>>();

        if ("lineData".equals(dataType)) {
            int startIndex = 0;
            for (int index = startIndex; index < dataLines.size(); index++) {
                String line = dataLines.get(index);
                if (StringUtil.isEmpty(line)) {
                    continue;
                }
                List<String> lineList = StringUtil.getContentList(line, Const.COMMA);
                if (Const.TRUE.equalsIgnoreCase(firstDataIsLabel)) {
                    // 获取第一列做为生成图片上面的Label
                    chartLabelArr.add(lineList.get(0));
                    // 并删除该列，不用做数据，因为这一行是说明
                    lineList.remove(0);
                } else {
                    chartLabelArr.add("Label " + (index + 1));
                }
                dataLinesList.add(lineList);
                if (lineList.size() > maxColumns) {// 取得最大的列数，根据情况补充label或者数据列
                    maxColumns = lineList.size();
                }
            }
        } else if ("columnData".equals(dataType)) {// 将列数据转换为行数据
            if (Const.TRUE.equalsIgnoreCase(firstDataIsLabel)) {
                // 取第一行做为Label
                String firstLine = dataLines.get(0);
                List<String> lineList = StringUtil.getContentList(firstLine, Const.COMMA);
                chartLabelArr.addAll(lineList);
                dataLines.remove(0);
            } else {
                String firstLine = dataLines.get(0);
                List<String> lineList = StringUtil.getContentList(firstLine, Const.COMMA);
                for (int i = 0; i < lineList.size(); i++) {
                    chartLabelArr.add("Label " + (i + 1));
                }
            }
            maxColumns = dataLines.size();
            StringBuilder sbLabel = new StringBuilder();
            for (int index = 0; index < dataLines.size(); index++) {
                String line = dataLines.get(index);
                if (StringUtil.isEmpty(line)) {
                    continue;
                }
                List<String> lineList = StringUtil.getContentList(line, Const.COMMA);
                int rows = lineList.size();
                int startIndex = 0;
                // 组装行数据
                for (int row = startIndex; row < rows; row++) {
                    List<String> rowDataList = getData(dataLinesList, row);
                    if (rowDataList == null) {
                        rowDataList = new ArrayList<String>();
                        dataLinesList.add(rowDataList);
                    }
                    rowDataList.add(lineList.get(row) == null ? "0" : lineList.get(row));
                }
            }
            if (sbLabel.length() > 0) {
                sbLabel.deleteCharAt(sbLabel.length() - 1);
                labels = sbLabel.toString();
            }
        }

        String[] labelsArr = labels.split(Const.COMMA);
        if (maxColumns < labelsArr.length) {
            maxColumns = labelsArr.length;
        }

        /** 组装生成的图的下面的Label(Begin) **/
        StringBuilder sbLabel = new StringBuilder();
        for (int i = 0; i < labelsArr.length; i++) {
            String label = labelsArr[i];
            if (StringUtil.isEmpty(label)) {
                sbLabel.append(Const.QUOTATION_MARK).append("Label ").append(i
                                                                             + 1).append(Const.QUOTATION_MARK).append(Const.COMMA);
            } else {
                sbLabel.append(Const.QUOTATION_MARK).append(label).append(Const.QUOTATION_MARK).append(Const.COMMA);
            }
        }
        // 如果给的Lable不够，则自动补充Lable，如补充"Lable 5"等
        for (int i = labelsArr.length; i < maxColumns; i++) {
            sbLabel.append(Const.QUOTATION_MARK).append("Label ").append(i
                                                                         + 1).append(Const.QUOTATION_MARK).append(Const.COMMA);
        }
        if (sbLabel.length() > 0) {
            sbLabel.deleteCharAt(sbLabel.length() - 1);
        }
        chartLabels = sbLabel.toString();
        /** 组装生成的图的下面的Label(End) **/

        /** 组装每一行的数据 **/
        for (List<String> lineList : dataLinesList) {
            StringBuilder dataLine = new StringBuilder();
            for (String column : lineList) {
                if (NumberUtil.isNumber(column)) {
                    dataLine.append(column).append(Const.COMMA);
                } else {
                    dataLine.append(0).append(Const.COMMA);
                }
            }
            // 对有些行中存在列数不够的情况下，全部补充数据为0
            for (int i = lineList.size(); i < maxColumns; i++) {
                dataLine.append(0).append(Const.COMMA);
            }
            if (dataLine.length() > 0) {
                dataLine.deleteCharAt(dataLine.length() - 1);
            }
            chartDatas.add(dataLine.toString());
        }

        return SUCCESS;
    }

    private List<String> getData(List<List<String>> dataLinesList, int index) {
        try {
            return dataLinesList.get(index);
        } catch (IndexOutOfBoundsException e) {
            return null;
        }
    }

    public String getLabels() {
        return labels;
    }

    public void setLabels(String labels) {
        this.labels = labels;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getDataType() {
        return dataType;
    }

    public void setDataType(String dataType) {
        this.dataType = dataType;
    }

    public String getFirstDataIsLabel() {
        return firstDataIsLabel;
    }

    public void setFirstDataIsLabel(String firstDataIsLabel) {
        this.firstDataIsLabel = firstDataIsLabel;
    }

    public String getChartType() {
        return chartType;
    }

    public void setChartType(String chartType) {
        this.chartType = chartType;
    }

    public String getChartLabels() {
        return chartLabels;
    }

    public List<String> getChartDatas() {
        return chartDatas;
    }

    public List<String> getFillColor() {
        return fillColor;
    }

    public String getColorsStr() {
        return colorsStr;
    }

    public List<String> getChartLabelArr() {
        return chartLabelArr;
    }

    public int getRandomExample() {
        return randomExample;
    }

}
